﻿using Base.FileRemoteModel.Down.Model;
using Basic.WxApiRemoteModel;
using Basic.WxApiService.WxModel.Platform;
using Basic.WxCollect;
using Basic.WxExtendService.Interface;
using Basic.WxLocalEvent.Interface;
using Basic.WxModel.DB;
using Basic.WxModel.WxMer;
using WeDonekRpc.Helper;

namespace Basic.WxLocalEvent.Service
{
    internal class WxMerService : IWxMerService
    {
        private readonly IWxMerCollect _WxMer;
        private readonly IFileApiService _File;
        private readonly IWxPlatformBindCollect _Bind;
        public WxMerService (
            IWxMerCollect wxMer,
            IWxPlatformBindCollect bnd,
            IFileApiService file)
        {
            this._Bind = bnd;
            this._WxMer = wxMer;
            this._File = file;
        }
        private long[] _UpImg (WxMerAdd mer)
        {
            List<long> fileId = new List<long>(2);
            if (mer.HeadUri.IsNotNull())
            {
                string name = string.Concat("WxHead_", mer.AppId, ".png");
                DownResult result = this._File.DownWxImage(new Uri(mer.HeadUri), "WxMerHead", name);
                if (result.IsSuccess)
                {
                    mer.HeadUri = result.FileUri.AbsoluteUri;
                    fileId.Add(result.FileId);
                }
            }
            if (mer.MerQrUri.IsNotNull())
            {
                string name = string.Concat("WxQr_", mer.AppId, ".png");
                DownResult result = this._File.DownWxImage(new Uri(mer.MerQrUri), "WxMerQr", name);
                if (result.IsSuccess)
                {
                    mer.MerQrUri = result.FileUri.AbsoluteUri;
                    fileId.Add(result.FileId);
                }
            }
            return fileId.ToArray();
        }
        private void _UpImg (WxMerSet mer, string appId, long? linkBizPk)
        {
            if (mer.HeadUri.IsNotNull())
            {
                string name = string.Concat("WxHead_", appId, ".png");
                DownResult result = this._File.DownWxImage(new Uri(mer.HeadUri), linkBizPk, "WxMerHead", name);
                if (result.IsSuccess)
                {
                    mer.HeadUri = result.FileUri.AbsoluteUri;
                }
            }
            if (mer.MerQrUri.IsNotNull())
            {
                string name = string.Concat("WxQr_", appId, ".png");
                DownResult result = this._File.DownWxImage(new Uri(mer.MerQrUri), linkBizPk, "WxMerQr", name);
                if (result.IsSuccess)
                {
                    mer.MerQrUri = result.FileUri.AbsoluteUri;
                }
            }
        }
        public void Add (WxMerInfo mer)
        {
            AuthorizerInfo info = mer.authorizer_info;
            WxMerAdd add = new WxMerAdd
            {
                WeChatId = info.alias,
                HeadUri = info.head_img,
                Show = info.signature,
                WxName = info.nick_name,
                AppId = mer.authorization_info.appid,
                MerQrUri = info.qrcode_url,
                OriginalId = info.user_name,
                PrincipalName = info.principal_name,
                AuthType = (WxAuthType)info.verify_type_info.id,
                MerType = (WxMerType)info.service_type_info.id,
                AccountStatus = (WxAccountStatus)info.account_status,
                RegisterType = info.register_type.HasValue ? (WxRegisterType)info.register_type.Value : null,
                BusinessState = this._GetState(info.business_info)
            };
            long[] fileId = this._UpImg(add);
            long wxMerId = this._WxMer.Add(add);
            if (!fileId.IsNull())
            {
                this._File.Save(fileId, wxMerId);
            }
        }

        private BusinessState _GetState (businessList bus)
        {
            int state = 0;
            byte open = 1;
            if (bus.open_pay == open)
            {
                state += (int)BusinessState.pay;
            }
            if (bus.open_scan == open)
            {
                state += (int)BusinessState.scan;
            }
            if (bus.open_shake == open)
            {
                state += (int)BusinessState.shake;
            }
            if (bus.open_store == open)
            {
                state += (int)BusinessState.store;
            }
            return (BusinessState)state;
        }

        public void Update (WxMerInfo mer, string appId)
        {
            string wxAppId = mer.authorization_info.appid;
            WxPrower[] prowers = mer.authorization_info.func_info.ConvertAll(a => (WxPrower)a.funcscope_category.id);
            DBWxMer wxMer = this._WxMer.Find(wxAppId);
            if (wxMer != null)
            {
                AuthorizerInfo info = mer.authorizer_info;
                WxMerSet set = new WxMerSet
                {
                    WeChatId = info.alias,
                    HeadUri = info.head_img,
                    Show = info.signature,
                    WxName = info.nick_name,
                    MerQrUri = info.qrcode_url,
                    PrincipalName = info.principal_name,
                    AuthType = (WxAuthType)info.verify_type_info.id,
                    AccountStatus = (WxAccountStatus)info.account_status,
                    BusinessState = this._GetState(info.business_info)
                };
                this._UpImg(set, wxMer.AppId, wxMer.Id);
                this._WxMer.Set(wxMer, set);
                this._File.Drop(new string[]
                {
                wxMer.HeadUri,
                wxMer.MerQrUri
                });
            }
            this._Bind.SetPrower(appId, wxAppId, prowers);
        }
    }
}